package com.cdtu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.RUserSpeciality;
import com.cdtu.entity.User;
import com.cdtu.mapper.CertifyMapper;
import com.cdtu.mapper.RUserCertifyMapper;
import com.cdtu.service.UserService;
import com.cdtu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Sakura
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-01-08 14:34:35
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CertifyMapper certifyMapper;

    @Autowired
    private RUserCertifyMapper rUserCertifyMapper;


    @Override
    public Map<String,Object> findByCategory(Integer pageNow, Integer pageSize, String grade, String certifyTypeId) {

        Map<String, Object> result = new HashMap<>();
//        根据证书类型查找证书，再查找证书的持有者
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        if ("".equals(grade)&&"".equals(certifyTypeId)){
//            都为空我们直接查询所有人员
//            page的详细情况
            Page<User> page = userMapper.selectPage(new Page<>(pageNow,pageSize),queryWrapper);
            List<User> records = page.getRecords();

            result.put("user",records);
            result.put("page",page);

            return result;

        }

        if ("".equals(grade)&&!("".equals(certifyTypeId))){

            List<Long> userIds=new ArrayList<>();
//            等级为空，但是证书类型不为空，我们就查找所有持该类证人员，不要未持证人员
            List<Long> certifyIds = certifyMapper.findByCertify_id(certifyTypeId);

//            根据证书id查询所有的持有者
            for (Long certifyId : certifyIds) {
                List<Long> userId = rUserCertifyMapper.findByCertify_id(certifyId);
                userIds.addAll(userId);
            }

            if (userIds.size()==0){
//                如果关系表中没人持有这一类的证书的人员，我们就返回一个空值
                List<User> list =new ArrayList<>();
                result.put("user",list);
                result.put("page",new Page<>());
                return result;
            }

//            得到所有的人员id之后，根据集合查询所有人员
            List<User> list = userMapper.selectBatchIds(userIds);
            int start = (pageNow-1)*pageSize;
            List<User> userList = list.stream().skip(start).limit(pageSize).collect(Collectors.toList());
            result.put("user",userList);

            long total = list.stream().count();
            Page<User> page=new Page<>();
            page.setTotal(total);
            page.setSize(pageSize);
            page.setCurrent(pageNow);

            result.put("page",page);

            return result;
        }


        if (!("".equals(grade))&&!("".equals(certifyTypeId))){
//            如果都不为空，需要将查询到的人员进行过滤
            List<Long> userIds=new ArrayList<>();
//            等级为空，但是类型不为空，我们就查找所有持该类证人员，不要未持证人员
            List<Long> certifyIds = certifyMapper.findByCertify_id(certifyTypeId);

//            根据证书id查询所有的持有者
            for (Long certifyId : certifyIds) {
                List<Long> userId = rUserCertifyMapper.findByCertify_id(certifyId);
                userIds.addAll(userId);
            }


//            得到所有的人员id之后，根据集合查询所有人员,这里有一个bug，那就是如果user的ID集合是空，就会报错，数据库就会报错，我们单独处理
            if (userIds.size()==0){
//                如果关系表中没人持有这一类的证书的人员，我们就返回一个空值
                List<User> list =new ArrayList<>();
                result.put("user",list);
                result.put("page",new Page<>());
                return result;
            }

            List<User> list = userMapper.selectBatchIds(userIds);
            int start = (pageNow-1)*pageSize;
            List<User> userList = list.stream().filter(user -> user.getGrade().equals(grade)).skip(start).limit(pageSize).collect(Collectors.toList());
            result.put("user",userList);

            long total = list.stream().count();
            Page<User> page=new Page<>();
            page.setTotal(total);
            page.setSize(pageSize);
            page.setCurrent(pageNow);
            result.put("page",page);
            return result;
        }

//        剩下:等级不为空，证书类型为空，查找所有等级符合的人员

        queryWrapper.eq(grade!=null,User::getGrade,grade);
        Page<User> page = userMapper.selectPage(new Page<>(pageNow,pageSize),queryWrapper);
//
        List<User> records = page.getRecords();

        result.put("user",records);
        result.put("page",page);

        return result;
    }


    @Override
    public List<Long> findIdAndGrade() {
        List<User> list = userMapper.findIdAndGrade();
        long countA = list.stream().filter(user -> "a".equals(user.getGrade())).count();
        long countB = list.stream().filter(user -> "b".equals(user.getGrade())).count();
        long countC = list.stream().filter(user -> "c".equals(user.getGrade())).count();

        List<Long> seriesData=new ArrayList<>();
        seriesData.add(countA);
        seriesData.add(countB);
        seriesData.add(countC);
        return seriesData;
    }

    @Override
    public List<Long> finUserByCertifyId(Long certifyId) {

        return userMapper.finUserByCertifyId(certifyId);
    }
}




